In [ ]:
import numpy as np
from bqplot import (OrdinalScale, LinearScale, Bars, 
                    Figure, Axis, ColorScale, ColorAxis, CATEGORY10)

In [ ]:
size = 100
np.random.seed(0)

x_data = range(size)
y_data = np.random.randn(size)
y_data_2 = np.random.randn(size)
y_data_3 = np.cumsum(np.random.randn(size) * 100.)

Basic Bar Chart


In [ ]:
x_ord = OrdinalScale()
y_sc = LinearScale()

bar = Bars(x=np.arange(10), y=np.random.rand(10), scales={'x': x_ord, 'y': y_sc})
ax_x = Axis(scale=x_ord)
ax_y = Axis(scale=y_sc, tick_format='0.2f', orientation='vertical')

Figure(marks=[bar], axes=[ax_x, ax_y], padding_x=0.025, padding_y=0.025)

Horizontal Bar Chart

To generate a horizontal bar chart, pass orientation='horizontal' to the bar.


In [ ]:
x_ord = OrdinalScale()
y_sc = LinearScale()

bar = Bars(x=np.arange(10), y=np.arange(-5, 5), scales={'x': x_ord, 'y': y_sc},
           orientation='horizontal')
ax_x = Axis(scale=x_ord, orientation='vertical')
ax_y = Axis(scale=y_sc, tick_format='0.2f')

Figure(marks=[bar], axes=[ax_x, ax_y], padding_x=0.025, padding_y=0.025)

Changing the reference value from which the Bars are drawn


In [ ]:
x_ord = LinearScale()
y_sc = LinearScale()

bar = Bars(x=x_data[:20], y=np.abs(y_data_2[:20]), scales={'x': x_ord, 'y': y_sc}, base=1.0)
ax_x = Axis(scale=x_ord)
ax_y = Axis(scale=y_sc, orientation='vertical', tick_format='0.2f')

Figure(marks=[bar], axes=[ax_x, ax_y])

In [ ]:
# changing the base
bar.base = 2.0

In [ ]:
bar.align = 'right'

Bar Chart Properties


In [ ]:
# Increasing the spacing between the bars
x_ord = OrdinalScale()
y_sc = LinearScale()

bar = Bars(x=x_data[:20], y=y_data[:20], scales={'x': x_ord, 'y': y_sc}, padding=0.2)
ax_x = Axis(scale=x_ord)
ax_y = Axis(scale=y_sc, orientation='vertical', tick_format='0.2f')

Figure(marks=[bar], axes=[ax_x, ax_y])

In [ ]:
# add bar labels
bar.label_display = True
bar.label_display_format = '.1f'
bar.label_font_style = {'fill': 'pink', 'font-size': '10px'}

In [ ]:
# changing basic properties like stroke and opacity
bar.stroke = 'orange'
bar.opacities = [0.5, 0.2]

In [ ]:
bar.orientation = 'horizontal'
ax_x.orientation = 'vertical'
ax_y.orientation = 'horizontal'

Stacked Bar Chart for 2-d data


In [ ]:
x_ord = OrdinalScale()
y_sc = LinearScale()

bar = Bars(x=x_data, y=[y_data[:20], y_data_2[:20]], 
           scales={'x': x_ord, 'y': y_sc}, padding=0.2,
           colors=CATEGORY10)
ax_x = Axis(scale=x_ord)
ax_y = Axis(scale=y_sc, orientation='vertical', tick_format='0.2f')

Figure(marks=[bar], axes=[ax_x, ax_y])

Grouped Bar Chart


In [ ]:
bar.type = 'grouped' # equivalent to saying 
# bar = Bars(x=x_data, y=y_data, scales={'x': x_ord, 'y': y_sc}, padding=0.2, type='grouped')

In [ ]:
x_ord = OrdinalScale()
y_sc = LinearScale()

bar = Bars(x=x_data, y=[y_data[:20], y_data_2[:20]], 
           scales={'x': x_ord, 'y': y_sc}, padding=0.2, 
           colors=CATEGORY10, type='stacked', orientation='horizontal')
ax_x = Axis(scale=x_ord, orientation='vertical')
ax_y = Axis(scale=y_sc, tick_format='0.2f')

Figure(marks=[bar], axes=[ax_x, ax_y])

In [ ]:
bar.type = 'grouped'

Modifying color mode


In [ ]:
## Color mode has 3 values. 'group', 'element' and 'no_group'. 
## 'group' means colors are assigned per group.
## 'element' means colors are assigned per group element.
## 'no_group' means means colors are assigned per bar, discarding the fact that there are groups or stacks
x_ord = OrdinalScale()
y_sc = LinearScale()

bar = Bars(x=x_data, y=[y_data[:20], y_data_2[:20]],
           scales={'x': x_ord, 'y': y_sc}, padding=0.2, 
           colors=CATEGORY10, color_mode='group')
ax_x = Axis(scale=x_ord)
ax_y = Axis(scale=y_sc, orientation='vertical', tick_format='0.2f')

Figure(marks=[bar], axes=[ax_x, ax_y])

In [ ]:
bar.color_mode = 'element'

In [ ]:
bar.color_mode = 'no_group'

In [ ]:
## for 1-d array for Y.
x_ord = OrdinalScale()
y_sc = LinearScale()

bar = Bars(x=x_data, y=y_data[:20], scales={'x': x_ord, 'y': y_sc}, padding=0.2,
           color_mode='element', labels=['Values'], 
           display_legend=True)
ax_x = Axis(scale=x_ord)
ax_y = Axis(scale=y_sc, orientation='vertical', tick_format='0.2f')

Figure(marks=[bar], axes=[ax_x, ax_y])

Representing additional dimension using Color


In [ ]:
# In this example, the color is just the magnitude of the y data
x_ord = OrdinalScale()
y_sc = LinearScale()
col_sc = ColorScale(scheme='Reds')

bar = Bars(x=x_data[:20], y=y_data[:20], color=np.abs(y_data[:20]),
           scales={'x': x_ord, 'y': y_sc, 'color': col_sc}, padding=0.2)
ax_x = Axis(scale=x_ord)
ax_y = Axis(scale=y_sc, orientation='vertical', tick_format='0.2f')
ax_c = ColorAxis(scale=col_sc, tick_format='0.2f')

margin = dict(top=50, bottom=80, left=50, right=50)

Figure(marks=[bar], axes=[ax_x, ax_y, ax_c], fig_margin=margin)

Adding color for 2-d data


In [ ]:
# By default color is applied along the axis=1
x_ord = OrdinalScale()
y_sc = LinearScale()
col_sc = ColorScale(scheme='Reds')

y_vals = [y_data[:20], y_data_2[:20], y_data_3[:20] / 100.0]
color_data = np.mean(y_vals, axis=1)

bar = Bars(x=x_data, y=y_vals, color=color_data, 
           scales={'x': x_ord, 'y': y_sc, 'color': col_sc}, padding=0.2,
           labels=['Dim 1', 'Dim 2', 'Dim 3'], display_legend=True)
ax_x = Axis(scale=x_ord)
ax_y = Axis(scale=y_sc, orientation='vertical', tick_format='0.2f')
ax_c = ColorAxis(scale=col_sc, tick_format='0.2f')

margin = dict(top=50, bottom=80, left=50, right=50)
Figure(marks=[bar], axes=[ax_x, ax_y, ax_c], fig_margin=margin)

In [ ]:
# Applying color along the axis=0
x_ord = OrdinalScale()
y_sc = LinearScale()
col_sc = ColorScale(mid=0.0)

y_vals = [y_data[:20], y_data_2[:20], y_data_3[:20] / 100.0]
color_data = np.mean(y_vals, axis=0)

bar = Bars(x=x_data, y=y_vals, color=color_data, 
           scales={'x': x_ord, 'y': y_sc, 'color': col_sc},
           padding=0.2, color_mode='group', stroke='orange')
ax_x = Axis(scale=x_ord)
ax_y = Axis(scale=y_sc, orientation='vertical', tick_format='0.2f')
ax_c = ColorAxis(scale=col_sc, tick_format='0.1f')

margin = dict(top=50, bottom=80, left=50, right=50)
Figure(marks=[bar], axes=[ax_x, ax_y, ax_c], fig_margin=margin)